var depotId, companyId, addressId, serverId;
var ind = 0;
var arrs = [],
	arrx = [],
	arrz = [];
var mon = 0;
var arrList2 = [];
var couponMoney = 0;
var PointMoney = 0;
var fa_money1 = 0;

function getInfo() {
	$.ajax({
		type: "get",
		url: "/h5/user_wareHouse_list",
		async: true,
		success: function(data) {
			var res = JSON.parse(data);
			for(var i = 0; i < res.data.length; i++) {
				if(depotId == res.data[i].id) {
					$(".depot_detial_list li").find("#depot1").html(res.data[i].name);
					$(".record .record_list").find("#depot1").html(res.data[i].name);
				}
			}

		}
	});
	$.ajax({
		type: "get",
		url: "/h5/ajax_get_expresscompany",
		async: true,
		success: function(data) {
			for(var i = 0; i < data.length; i++) {
				if(companyId == data[i].id) {
					$(".depot_detial_list li").find("#company1").html(data[i].name);
					$(".record .record_list").find("#company1").html(data[i].name);
				}
			}
		}
	});
}

function getInfo2() {
	arrs = [];
	$.ajax({
		type: "get",
		url: "/h5/getAddressById",
		async: true,
		data: {
			"id": addressId
		},
		success: function(data) {
			var res = JSON.parse(data);
			if(res.data == undefined) {
				addcity = "";
			} else {
				addcity = res.data.address + res.data.addressDetails;
			}
			arrs.push(addcity)
			$(".waybill").eq(ind).find("li").each(function(i) {
				$(this).find("#way_address").html(arrs[i]);
			})
			$("#selectPack").find("#way_address").html(addcity);
		}
	});
}
var arrd = [];

function getInfo3() {
	$.ajax({
		type: "get",
		url: "/h5/ajax_get_addedservice.do",
		async: true,
		success: function(data) {
			console.log(arrx)
			arrz = [];
			if(arrx != undefined) {
				for(var j = 0; j < arrx.length; j++) {
					var zz = "";
					if(arrx[j] != undefined) {
						for(var k = 0; k < arrx[j].length; k++) {
							for(var n = 0; n < data.length; n++) {
								if(arrx[j][k] == data[n].id) {
									arrz[j] = zz += data[n].name + "、";
								}
							}
						}
					} else {
						arrz[j] = "";
					}
				}
			}else{
			arrz = [];	
			}
			$(".waybill").eq(ind).find("li").each(function(i) {
				$(this).find("#way_server").html(arrz[i]);
			});
			$("#selectPack").find("#way_server").html(arrz);
			arrx = [];
			console.log(arrx)
		}
	});
}

function getCatory() {
	$.ajax({
		type: "get",
		url: "/h5/ajax_get_category_by_parentid",
		async: false,
		success: function(data) {
			$(".goods").each(function() {
				for(var i = 0; i < data.length; i++) {
					if($(this).attr("smId") == data[i].id) {
						$(this).find("#smallCatory").html(data[i].name);
						$(this).attr("bgId", data[i].parentId);
					}
				}
			});
			$(".package_detial_content .package_detial_list").each(function() {
				for(var i = 0; i < data.length; i++) {
					if($(this).attr("smId") == data[i].id) {
						$(this).find("#smallCatory").html(data[i].name);
						$(this).attr("bgId", data[i].parentId);
					}
				}
			});
			$("tbody #outTr").each(function() {
				for(var i = 0; i < data.length; i++) {
					if($(this).attr("sId") == data[i].id) {
						$(this).find("#smallCatory").html(data[i].name);
						$(this).attr("bId", data[i].parentId);
					}
				}
			});
			$("#fa_detial .deliver_detial_content").each(function() {
				for(var i = 0; i < data.length; i++) {
					if($(this).attr("sId") == data[i].id) {
						$(this).find("#smallCatory").html(data[i].name);
						$(this).attr("bId", data[i].parentId);
					}
				}
			});
			$("#Recproducts .depot_list_box").each(function() {
				for(var i = 0; i < data.length; i++) {
					if($(this).attr("smid") == data[i].id) {
						$(this).find("#smallCatory").html(data[i].name);
						$(this).attr("bgid", data[i].parentId);
					}
				}
			});
		}
	});
	$.ajax({
		type: "get",
		url: "/h5/ajax_get_category?leaf=2",
		async: false,
		success: function(data) {
			$(".goods").each(function() {
				for(var i = 0; i < data.length; i++) {
					if($(this).attr("bgId") == data[i].id) {
						$(this).find("#bigCatory").html(data[i].name);
					}
				}
			});
			$(".package_detial_content .package_detial_list").each(function() {
				for(var i = 0; i < data.length; i++) {
					if($(this).attr("bgId") == data[i].id) {
						$(this).find("#bigCatory").html(data[i].name);
					}
				}
			});
			$("tbody #outTr").each(function() {
				for(var i = 0; i < data.length; i++) {
					if($(this).attr("bId") == data[i].id) {
						$(this).find("#bigCatory").html(data[i].name);
					}
				}
			});
			$("#fa_detial .deliver_detial_content").each(function() {
				for(var i = 0; i < data.length; i++) {
					if($(this).attr("bId") == data[i].id) {
						$(this).find("#bigCatory").html(data[i].name);
					}
				}
			});
			$("#Recproducts .depot_list_box").each(function() {
				for(var i = 0; i < data.length; i++) {
					if($(this).attr("bgid") == data[i].id) {
						$(this).find("#bigCatory").html(data[i].name);
					}
				}
			});
		}
	});
}

function slide() {
	$(".goods").each(function() {
		$(this).find(".retract").toggle(function() {
			$(this).attr("src", "img/retract_1.png");
			$(this).parent().parent().parent().children(".depot_detial_list_open").slideUp();
		}, function() {
			$(this).attr("src", "img/retract.png");
			$(this).parent().parent().parent().children(".depot_detial_list_open").slideDown();
		})
	})
}
//某个认领包裹信息
var packmoney3 = 0,
	packmoney4 = 0,
	Fmoney1 = 0;

function packageInfo1() {
	$.ajax({
		type: "get",
		url: "/h5/all_wait_claim_putpackage",
		async: true,
		data: {
			"id": ishave
		},
		success: function(data) {
			var res = JSON.parse(data);
			console.log(res)
			var isImg = false;
			var reciveList = res.data.putPackageLists;
			for(var i = 0; i < reciveList.length; i++) {
				if(ishave == reciveList[i].id) {
					var addedTimes = reciveList[i].addedTimes;
					$(".weight").html()
					$(".etc_num").html(reciveList[i].expressNumber);
					$("#weight").html(reciveList[i].budgetWeigh + "kg");

				}
			}
		}
	});
}
//地址
function address_id() {
	$.ajax({
		type: "post",
		url: "/h5/getUserMessage",
		async: true,
		success: function(data) {
			var res = JSON.parse(data);
			var defaultAddressId = res.data.defaultAddressId;
			$.ajax({
				type: "post",
				url: "/h5/getAddressById",
				async: true,
				data: {
					"id": defaultAddressId
				},
				success: function(data) {
					var res = JSON.parse(data)
					if(res.data == undefined) {
						$(".pre_write_status").hide();
						$(".pre_write_info1 span").eq(2).hide();
					} else {
						var str = res.data.address;
						str = str.replace(/\>/g, ' ');
						var phone = res.data.consigneePhone;
						phone = phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
						var idcard = res.data.idCard;
						idcard = idcard.replace(/^(.{6})(?:\d+)(.{4})$/, "$1********$2");
						$(".pre_write_info1 span").eq(0).html(res.data.consignee);
						$(".pre_write_info1 span").eq(1).html(phone);
						$(".pre_write_info1 span").eq(2).html(res.data.alias);
						$(".pre_write_num").html(idcard);
						$(".pre_write_address").html(str + res.data.addressDetails);
						$(".pre_write").attr("addressid", defaultAddressId);
						$(".pre_write_status").show();
						$(".pre_write_info1 span").eq(2).show();
					}
				}
			});
		}
	})
}
//增值服务
function outSever() {
	var levelId;
	$.ajax({
		type: "post",
		url: "/h5/getUserMessage",
		async: true,
		success: function(data) {
			var res = JSON.parse(data);
			levelId = res.data.levelId;
		}
	});
	$.ajax({
		type: "get",
		url: "/h5/ajax_get_addedservice.do",
		async: true,
		data: {
			"type": 1
		},
		success: function(data) {
			for(var i = 0; i < data.length; i++) {
				var strServer = '<li serverId=' + data[i].id + '>' +
					'<div class="sever_img">' +
					'<img src=' + data[i].icon + ' />' +
					'</div>' +
					'<div class="sever_title flex">' +
					'<span>' + data[i].name + '</span>' +
					'<img class="beizhu" src="img/beizhu3.png" />' +
					'</div>' +
					'<div class="chargeMoney"></div>' +
					'<div class="sever_check">' +
					'<img src="img/right1.png" />' +
					'</div>' +
					'<div class="sever_state">' +
					'<img src="img/sever-icon1.png" />' +
					'<div id="isCharge">免费</div>' +
					'</div>' +
					'</li>';
				$("#pre_write_llist2").append(strServer);
				if(data[i].isCharge == false) {
					$("#pre_write_llist2 li").eq(i).children(".sever_state").children("#isCharge").html("免费");
				} else {
					if(levelId != "") {
						$("#pre_write_llist2 li").eq(i).children(".sever_state").children("#isCharge").html("收费");
						$("#pre_write_llist2 li").eq(i).children(".chargeMoney").html(data[i].vipPrice + "元");
					} else {
						$("#pre_write_llist2 li").eq(i).children(".sever_state").children("#isCharge").html("收费");
						$("#pre_write_llist2 li").eq(i).children(".chargeMoney").html(data[i].defaultPrice + "元");
					}
				}
			}
			sever3();
		}
	});
}
//增值服务
function sever3() {
	$("#pre_write_llist2 li").each(function(i) {
		$(this).children(".sever_check").on("click", function() {
			couponPoint();
			if($(this).is(".checked")) {
				$(this).removeClass("checked");
				$(this).children().attr("src", "img/right1.png");
				$(this).parent().find(".sever_state").children("img").attr("src", "img/sever-icon1.png");
				var serverTxt = $(this).parent().children(".sever_state").children("#isCharge").html();
				var serverTxt2 = $(this).parent().children(".sever_title").children("span").html();
				if(serverTxt2 == "礼品卡刮开") {
					$(".card_num").css("display", "none");
					$(".card_num .card_number").html(0);
				}
				if(serverTxt != "免费") {
					var str = $(this).parent().children(".chargeMoney").html();
					var strm = $("#fa_money1").html();
					var totalMoney1 = str.split("元").join("");
					fa_money1 = strm.split("元").join("");
					mon = sub(mon, Number(totalMoney1));
				}
				var monSum1 = add(mon, Number(fa_money1));
				var monSum2 = add(couponMoney, PointMoney);
				var monSum3 = sub(monSum1, monSum2);
				$("#fa_money2").html(mon + "元");
				$("#fa_money3").html(monSum3 + "元");
				$("#fa_money4").html($("#fa_money3").html());
			} else {
				$(this).addClass("checked");
				$(this).children().attr("src", "img/sever_sel.png");
				var serverTxt = $(this).parent().children(".sever_state").children("#isCharge").html();
				var serverTxt2 = $(this).parent().children(".sever_title").children("span").html();
				if(serverTxt2 == "礼品卡刮开") {
					$(".card_num").css("display", "flex");
					$(".card_num .card_number").html(1);
				}
				var strm = $("#fa_money1").html();
				fa_money1 = strm.split("元").join("");
				if(serverTxt == "免费") {
					$(this).parent().find(".sever_state").children("img").attr("src", "img/sever-icon2.png");
				} else {
					$(this).parent().find(".sever_state").children("img").attr("src", "img/sever-icon3.png");
					var str = $(this).parent().children(".chargeMoney").html();
					var totalMoney1 = str.split("元").join("");
					mon = add(mon, Number(totalMoney1));
				}
				var monSum1 = add(mon, Number(fa_money1));
				var monSum2 = add(couponMoney, PointMoney);
				var monSum3 = sub(monSum1, monSum2);
				$("#fa_money2").html(mon + "元");
				$("#fa_money3").html(monSum3 + "元");
				$("#fa_money4").html($("#fa_money3").html());
			}
		})
	})
}

function quanFen() {
	$.ajax({
		type: "post",
		url: "/h5/getUserCoupons",
		async: true,
		success: function(data) {
			var res = JSON.parse(data)
			var couponUser = res.data.userCoupons;
			var couponList = res.data.couponMap;
			for(var i = 0; i < couponUser.length; i++) {
				if(couponUser[i].status == 2) {
					var id = couponUser[i].couponId;
					var optionHtml = '<option value=' + couponUser[i].couponId + '>' + couponList[id].name + '</option>';
					$(".couponHtml").append(optionHtml);
				}

			}
		}
	});
	$.ajax({
		type: "post",
		url: "/h5/getUserMessage",
		async: true,
		success: function(data) {
			var res = JSON.parse(data)
			$(".can_use").html(res.data.integral);
		}
	});
}
//判断积分优惠券
function couponPoint() {
	var integral = $(".point_input input").val();
	var couponId = $(".couponHtml").val();
	var channelId = $(".C_cheched").attr("chid");
	var totalMoney = Number(($("#fa_money1").html()).replace('元', '')) + Number(($("#fa_money2").html()).replace('元', ''));
	$.ajax({
		type: "post",
		url: "/h5/afresh_wx_fee.do",
		async: true,
		data: {
			"integral": integral,
			"couponId": couponId,
			"channelId": channelId,
			"totalMoney": totalMoney

		},
		success: function(data) {
			var res = JSON.parse(data);
			if(res.code == 0) {
				couponMoney = Number(res.data.couponMoney);
				PointMoney = Number(res.data.integral);
				var monSum1 = add(mon, Number(fa_money1));
				var monSum2 = add(couponMoney, PointMoney);
				var monSum3 = sub(monSum1, monSum2);
				console.log(monSum3)
				$("#fa_money3").html(monSum3 + "元");
				$("#fa_money4").html($("#fa_money3").html());
			} else {
				$("#deliverTips .tips").show();
				$("#deliverTips .tips").html(res.err);
				setTimeout(function() {
					$("#deliverTips .tips").hide();
				}, 1000);
				PointMoney = 0;
				couponMoney = 0;
				var monSum1 = add(mon, Number(fa_money1));
				var monSum2 = add(couponMoney, PointMoney);
				var monSum3 = sub(monSum1, monSum2);
				console.log(monSum3)
				$("#fa_money3").html(monSum3 + "元");
				$("#fa_money4").html($("#fa_money3").html());
			}
		}
	});
}